// pages/operation/classRecord/classRecordIndex/index.js
import { ClassModel } from "../../../../api/api-work/classModel.js";
import { timestampToTime } from "../../../../utils/index.js";
const classModel = new ClassModel();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeTab: "first",
    // 点名记录
    showCallDate: false, // 选择年月
    currentCallDate: "",
    currentCallDateParam: "", // 选择年月显示
    minDate: new Date(2020, 0).getTime(),
    firstPageNum: 1,
    firstTotal: 0,
    firstTempDatas: [],
    firstDataObj: {},
    isFirstTriggered: false,
    firstLoadStatus: "",
    // 超时未点
    secondPageNum: 1,
    secondTotal: 0,
    secondTempDatas: [],
    secondDataObj: {},
    isSecondTriggered: false,
    secondLoadStatus: "",
    // 缺课记录
    thirdPageNum: 1,
    thirdTotal: 0,
    thirdTempDatas: [],
    thirdDataObj: {},
    isThirdTriggered: false,
    thirdLoadStatus: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // // 获取点名记录
    // await this.getOrdeList();
    // // 获取超时未点
    // await this.getOverTimeList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    this.setData({
      isFirstTriggered: false,
      firstPageNum: 1,
      firstDataObj: {},
      firstTempDatas: [],
      isSecondTriggered: false,
      secondPageNum: 1,
      secondDataObj: {},
      secondTempDatas: [],
      isThirdTriggered: false,
      thirdPageNum: 1,
      thirdDataObj: {},
      thirdTempDatas: [],
    });
    // 获取点名记录
    await this.getOrdeList();
    // 获取超时未点
    await this.getOverTimeList();
    // 获取缺课记录
    await this.getOrderListSecond();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  // 选择年月
  changeCallDate() {
    this.setData({
      showCallDate: true,
      currentCallDate: new Date().getTime(),
    });
  },

  clearCallDate() {
    this.setData({
      showCallDate: false,
      currentCallDate: "",
      currentCallDateParam: "",
      firstPageNum: 1, // 筛选页数重置
      firstDataObj: {},
      firstTempDatas: [],
    });
    this.getOrdeList();
  },
  onCloseCallDate() {
    this.setData({
      showCallDate: false,
    });
  },
  // 确认年月
  chooseCallDate(e) {
    this.setData({
      showCallDate: false,
      currentCallDate: new Date(timestampToTime(e.detail)).getTime(),
      currentCallDateParam: timestampToTime(e.detail),
      firstPageNum: 1, // 筛选页数重置
      firstDataObj: {},
      firstTempDatas: [],
    });
    this.getOrdeList();
  },
  // 获取点名记录
  async getOrdeList() {
    classModel
      .getOrdeListApi({
        listType: 1,
        callStartTime: this.data.currentCallDateParam,
        callEndTime: this.data.currentCallDateParam,
        pageNum: this.data.firstPageNum,
        pageSize: 15,
      })
      .then((res) => {
        if (res.rows.length > 0) {
          res.rows.forEach((item) => {
            item["callTimeNew"] = item.callTime.split(" ")[1];
          });
        }
        let rows = this.data.firstTempDatas;
        rows.push(...res.rows);
        this.setData({
          firstTempDatas: rows,
          firstTotal: res.total,
        });
        // 数据格式整合
        let obj = {};
        this.data.firstTempDatas.forEach((item) => {
          const time = item.callTime.split(" ")[0];
          if (obj.hasOwnProperty(time)) {
            obj[time].push(item);
          } else {
            obj[time] = [item];
          }
        });
        this.setData({
          firstDataObj: obj,
        });

        if (this.data.firstTotal == 0) {
          this.setData({
            // 无数据
            firstLoadStatus: "",
          });
        } else if (this.data.firstTempDatas.length < this.data.firstTotal) {
          //
          this.setData({
            firstLoadStatus: "loadmore",
          });
        } else {
          this.setData({
            firstLoadStatus: "nomore",
          });
        }
      });
  },
  getMoreCalls() {
    if (this.data.firstLoadStatus === "nomore") {
      // 没有更多了
      return;
    }
    this.setData({
      firstPageNum: this.data.firstPageNum + 1,
      firstLoadStatus: "loading",
    });
    this.getOrdeList();
  },
  refreshMoreCalls() {
    this.setData({
      isFirstTriggered: false,
      firstPageNum: 1,
      firstDataObj: {},
      firstTempDatas: [],
    });
    this.getOrdeList();
  },
  // 去点名详情
  tapClassRecord(e) {
    const param = JSON.stringify(e.currentTarget.dataset.query);
    wx.navigateTo({
      url: `/pages/operation/classRecord/classRecordDetail/index?param=${param}`,
    });
  },

  // 获取超时未点
  async getOverTimeList() {
    classModel
      .getOrdeListApi({
        listType: 2,
        pageNum: this.data.secondPageNum,
        pageSize: 15,
      })
      .then((res) => {
        let rows = this.data.secondTempDatas;
        rows.push(...res.rows);
        this.setData({
          secondTempDatas: rows,
          secondTotal: res.total,
        });
        // 数据格式整合
        let obj = {};
        this.data.secondTempDatas.forEach((item) => {
          if (obj.hasOwnProperty(item.classDate)) {
            obj[item.classDate].push(item);
          } else {
            obj[item.classDate] = [item];
          }
        });
        this.setData({
          secondDataObj: obj,
        });

        if (this.data.secondTotal == 0) {
          this.setData({
            // 无数据
            secondLoadStatus: "",
          });
        } else if (this.data.secondTempDatas.length < this.data.secondTotal) {
          //
          this.setData({
            secondLoadStatus: "loadmore",
          });
        } else {
          this.setData({
            secondLoadStatus: "nomore",
          });
        }
      });
  },
  getMoreOvertimes() {
    if (this.data.secondLoadStatus === "nomore") {
      // 没有更多了
      return;
    }
    this.setData({
      secondPageNum: this.data.secondPageNum + 1,
      secondLoadStatus: "loading",
    });
    this.getOverTimeList();
  },
  refreshMoreOvertimes() {
    this.setData({
      isSecondTriggered: false,
      secondPageNum: 1,
      secondDataObj: {},
      secondTempDatas: [],
    });
    this.getOverTimeList();
  },
  // 去点名
  gotoCall(e) {
    wx.navigateTo({
      url: `/pages/operation/class/classOrder/index?id=${e.currentTarget.dataset.query.id}&orderNo=${e.currentTarget.dataset.query.orderNo}&idsStr=${e.currentTarget.dataset.query.courseIds}&classDate=${e.currentTarget.dataset.query.classDate}&cofId=${e.currentTarget.dataset.query.cofId}`,
    });
  },

  // 获取缺课记录
  getOrderListSecond() {
    classModel
      .getOrderListSecondApi({ pageNum: this.data.thirdPageNum, pageSize: 15 })
      .then((res) => {
        let rows = this.data.thirdTempDatas;
        rows.push(...res.rows);
        this.setData({
          thirdTempDatas: rows,
          thirdTotal: res.total,
        });
        // 数据格式整合
        let obj = {};
        this.data.thirdTempDatas.forEach((item) => {
          const time = item.callTime.split(" ")[0];
          if (obj.hasOwnProperty(time)) {
            obj[time].push(item);
          } else {
            obj[time] = [item];
          }
        });
        this.setData({
          thirdDataObj: obj,
        });

        if (this.data.thirdTotal == 0) {
          this.setData({
            // 无数据
            thirdLoadStatus: "",
          });
        } else if (this.data.thirdTempDatas.length < this.data.thirdTotal) {
          //
          this.setData({
            thirdLoadStatus: "loadmore",
          });
        } else {
          this.setData({
            thirdLoadStatus: "nomore",
          });
        }
      });
  },
  getMoreNoCalls() {
    if (this.data.thirdLoadStatus === "nomore") {
      // 没有更多了
      return;
    }
    this.setData({
      thirdPageNum: this.data.thirdPageNum + 1,
      thirdLoadStatus: "loading",
    });
    this.getOrderListSecond();
  },
  refreshMoreNoCalls() {
    this.setData({
      isThirdTriggered: false,
      thirdPageNum: 1,
      thirdDataObj: {},
      thirdTempDatas: [],
    });
    this.getOrderListSecond();
  },
  // 跳转缺课详情
  jumpToLostDetail(e) {
    console.log("ww: ", e);
    const param = JSON.stringify(e.currentTarget.dataset.query);
    wx.navigateTo({
      url: `/pages/operation/classRecord/classRecordLostDetail/index?param=${param}`,
    });
  },
  // 开班补课
  makeUpMissedLesson() {
    wx.navigateTo({
      url: `/pages/operation/classRecord/classMakeUp/index`,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
